/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.plugins;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.grayrabbit.cms.entity.Vote;
import org.grayrabbit.cms.entity.VoteOption;
import org.grayrabbit.cms.service.plugins.VoteOptionService;
import org.grayrabbit.cms.vo.plugins.VoteOptionBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.web.struts2.GenericAction;

/**
 * <b>VoteOptionAction</b> Struts2 action�?





 * @version 1.0,创建时间:2011-05-16 16:08:31 
 */
@Controller
@Scope("prototype")
public class VoteOptionAction extends GenericAction<VoteOptionBean> {
	private static final long serialVersionUID = -1L;
	//Service接口接入
	@Autowired
	@Qualifier("voteOptionServiceImpl")
	private VoteOptionService voteOptionService;
	
	//VO对象，由前端页面数据组装
	private VoteOptionBean voteOption;
	
	private Vote vote;

	private String voteId;
	
	public Vote getVote() {
		return vote;
	}

	public void setVote(Vote vote) {
		this.vote = vote;
	}
	
	public String getVoteId() {
		return voteId;
	}

	public void setVoteId(String voteId) {
		this.voteId = voteId;
	}
	/**return 前端页面数据组装VO对象,<b>VoteOptionBean</b>*/
	public VoteOptionBean getVoteOption() {
		return voteOption;
	}

	public void setVoteOption(VoteOptionBean voteOption) {
		this.voteOption = voteOption;
	}
	
	//分页对象
	private Page<VoteOption> page = new Page<VoteOption>(MAX_PAGE_NUM,true);
	
	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<VoteOption> getPage() {
		return page;
	}
	//接收前端form,url提交参数组装成分页property
	public void setPage(Page<VoteOption> page) {
		this.page = page;
	}
	
	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList(){
		prepareModel();
	}
	
	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare(){
		if(voteOption==null){
			voteOption = new VoteOptionBean();
		}
	}
	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法,
	 *  如：
	 *	<pre>
	 *		<code> name="user.userName"</code>
	 *	改写成：
	 *		<code> name="userName"</code>
	 *	action实例中，会自动组成user成员变量
	 *	</pre>
	 */
	public VoteOptionBean getModel() {
		return voteOption;
	}
	
	@Override
	protected void prepareModel(){
		if(StringUtils.isNotBlank(voteOption.getVoteOptionId())){
			voteOption = (VoteOptionBean)dozer.map(voteOptionService.getVoteOption(voteOption.getVoteOptionId()),VoteOptionBean.class);
		}
		
	}

	/**
	 * 删除<b>VoteOption</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到voteoption-list.action
	 */
	@Override
	@Action(value="voteoption-delete",
			results={@Result(name="reload",type="redirectAction",params={"actionName","voteoption-list"})}
			)
	public String delete(){
		voteOptionService.delete(key);
		return RELOAD;
	}
	@Action(value="voteoption-del")
	public String del(){
		voteOptionService.delete(key);
		return null;
	}
	/**
	 * VoteOption的列表方法，通过成员变量<b>page�?{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到voteoption-list.jsp页面
	 */
	@Override
	@Action(value="voteoption-list")
	public String list(){
		voteOptionService.list(page,voteOption);
		return SUCCESS;
	}
	/**
	 * 根据投票主题id查询投票选项
	 * @return
	 */
	@Action(value="voteoption-allList")
	public String allList(){
		String text = "";
		voteId = vote.getVoteId();
		List<VoteOption> list = voteOptionService.findByVoteId(voteId);
		if(list.size()>0){
			text = "yes";
		}else{
			text = "no";
		}
		return renderText(text);
	}
	/**
	 * VoteOption的新增与编辑的方法执行成功能
	 * 将映射到voteoption-input.jsp
	 */
	@Action(value="voteoption-input")
	public String input(){
		prepareModel();
		return SUCCESS;
	}
	
	/**
	 * save or update VoteOption的方法，将成员property voteOption转成VoteOption实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到voteoption-list.action
	 */
	@Override
	@Action(value="voteoption-save",
			results={@Result(name="reload",type="redirectAction",params={"actionName","voteoption-list"})}
		)
	public String save(){
		VoteOption obj = (VoteOption) dozer.map(voteOption, VoteOption.class);
		if(!StringUtils.isNotBlank(obj.getVoteOptionId())){
			obj.setVoteOptionId(null);
		}
		voteOptionService.save(obj);
		return RELOAD;
	}
}